﻿package cn.flashk.map
{
    import cn.flashk.zipPack.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;

    public class MapEngine extends Sprite
    {
        public var keys:Array;
        private var ftzip:FileTxtZiperDecoder;
        private var background:Background;
        private var layer:DynamicLayer;
        private var sky:SkyWeather;
        private var xml:XML;
        private var isInit:Boolean;
        private var newX:Number;
        private var newY:Number;
        private var _viewSize:Rectangle;
        private static var ins:MapEngine;

        public function MapEngine()
        {
            keys = [false, false, false, false];
            ins = this;
            opaqueBackground = 0;
            ftzip = new FileTxtZiperDecoder();
            ftzip.addEventListener(FileTxtZiperDecoder.DECODE_COMPLETE, initMapData);
            background = new Background();
            layer = new DynamicLayer();
            sky = new SkyWeather();
            this.addChild(background);
            this.addChild(layer);
            this.addChild(sky);
            this.addEventListener(Event.ADDED_TO_STAGE, initStageLis);
            return;
        }// end function

        public function load(url:String) : void
        {
            isInit = false;
            ftzip.loadAndDecodeFile(url);
            return;
        }// end function

        public function get backgroundLayer() : Background
        {
            return background;
        }// end function

        public function get mapWidth() : Number
        {
            return 1920;
        }// end function

        public function get mapHeight() : Number
        {
            return 1080;
        }// end function

        public function get centerX() : Number
        {
            return newX;
        }// end function

        public function get centerY() : Number
        {
            return newY;
        }// end function

        public function set viewSize(param1:Rectangle) : void
        {
            _viewSize = param1;
            return;
        }// end function

        public function get viewSize() : Rectangle
        {
            return _viewSize;
        }// end function

        public function getPoint(param1:uint) : Point
        {
            var _loc_2:* = new Point();
            _loc_2.x = int(newX - _viewSize.width / 2);
            _loc_2.y = int(newY - _viewSize.height / 2);
            return _loc_2;
        }// end function

        public function move(param1:Number, param2:Number) : void
        {
            this.x = this.x - param1;
            this.y = this.y - param2;
            if (this.x > 0)
            {
                this.x = 0;
            }
            if (this.y > 0)
            {
                this.y = 0;
            }
            if (this.x < _viewSize.width - mapWidth)
            {
                this.x = _viewSize.width - mapWidth;
            }
            if (this.y < _viewSize.height - mapHeight)
            {
                this.y = _viewSize.height - mapHeight;
            }
            return;
        }// end function

        public function moveTo(px:Number, py:Number) : void
        {
            newX = px;
            newY = py;
            checkRectangle();
            this.x = int(-getPoint(1).x);
            this.y = int(-getPoint(1).y);
            return;
        }// end function

        private function checkRectangle() : void
        {
            if (newX < int(_viewSize.width / 2))
            {
                newX = int(_viewSize.width / 2);
            }
            if (newY < int(_viewSize.height / 2))
            {
                newY = int(_viewSize.height / 2);
            }
            if (newX > int(mapWidth - _viewSize.width / 2))
            {
                newX = int(mapWidth - _viewSize.width / 2);
            }
            if (newY > int(mapHeight - _viewSize.height / 2))
            {
                newY = int(mapHeight - _viewSize.height / 2);
            }
            return;
        }// end function

        private function initMapData(event:Event) : void
        {
            xml = new XML(ftzip.data);
            background.init(xml.background[0]);
            layer.initObjects(xml.objects[0]);
            isInit = true;
            return;
        }// end function

        private function initStageLis(event:Event) : void
        {
            this.stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeyDown);
            this.stage.addEventListener(KeyboardEvent.KEY_UP, checkKeyUp);
            return;
        }// end function

        private function checkKeyDown(event:KeyboardEvent) : void
        {
            switch(event.keyCode)
            {
                case 39:
                {
                    keys[2] = true;
                    break;
                }
                case 37:
                {
                    keys[1] = true;
                    break;
                }
                case 38:
                {
                    keys[3] = true;
                    break;
                }
                case 40:
                {
                    keys[4] = true;
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        private function checkKeyUp(event:KeyboardEvent) : void
        {
            switch(event.keyCode)
            {
                case 39:
                {
                    keys[2] = false;
                    break;
                }
                case 37:
                {
                    keys[1] = false;
                    break;
                }
                case 38:
                {
                    keys[3] = false;
                    break;
                }
                case 40:
                {
                    keys[4] = false;
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        public static function getInstance() : MapEngine
        {
            return ins;
        }// end function

    }
}
